//
//  HomeView.swift
//  DesignCode
//
//  Created by 刘文超 on 2020/5/8.
//  Copyright © 2020 刘文超. All rights reserved.
//

import SwiftUI

struct HomeView: View {
    @Binding var showPerson : Bool
    @State var showContentView = false
    var body: some View {
        VStack{
            HStack{
                Text("Watching")
                    .font(.largeTitle)
                    .fontWeight(.bold)
                Spacer()
                
                AvaterView(showPerson: $showPerson)
                
                
                Button(action: { self.showContentView.toggle() }){
                    Image(systemName: "bell")
                        .renderingMode(.original)
                        .font(.system(size: 16, weight: .medium))
                        .frame(width: 36, height: 36)
                        .background(Color.white)
                        .clipShape(Circle())
                        .shadow(color: Color.black.opacity(0.2), radius: 10, x: 0, y: 10)
                    
                }
                .sheet(isPresented: $showContentView) {
                    updateList()
                }
            }
            .padding(.horizontal)
            .padding(.top,30)
            .padding(.leading,14)
            
            
            ScrollView(.horizontal,showsIndicators: false) {
                HStack ( spacing: 30){
                    ForEach(dataArray) { item in
                        GeometryReader { geometry in
                            CardSectionItemView(model: item)
                                .rotation3DEffect(Angle(degrees: Double(geometry.frame(in: .global).minX - 30) / 20), axis: (x: 0, y: 10, z: 0))
                        }
                        .frame(width: 275, height: 275)
                    }
                }
                .background(Color.white)
                .padding()
                .padding(.bottom,20)
            }

            
            
            
            Spacer()
        }
    }
}

//MARK:创建模型
struct SecondModel:Identifiable {
    
    var id = UUID()
    var title:String
    var content:String
    var logo:String
    var contentImage:Image
    var bgColor:Color
}
//MARK:创建数据
let dataArray = [
    SecondModel(title: "Prototype designs in SwiftUI", content: "15 sections", logo: "Logo1", contentImage: Image("Card1"), bgColor: Color("card1")),
    SecondModel(title: "Prototype designs in SwiftUI", content: "18 sections", logo: "Logo2", contentImage: Image("Card2"), bgColor: Color("card2")),
    SecondModel(title: "Prototype designs in SwiftUI", content: "18 sections", logo: "Logo3", contentImage: Image( "Card3"), bgColor: Color("card3")),
    SecondModel(title: "Prototype designs in SwiftUI", content: "18 sections", logo: "Logo1", contentImage: Image( "Card4"), bgColor: Color("card4")),
    SecondModel(title: "Prototype designs in SwiftUI", content: "18 sections", logo: "Logo2", contentImage: Image("Card5"), bgColor: Color("card5"))
    
]

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView(showPerson: .constant(false))
    }
}

struct CardSectionItemView: View {
    
    var model:SecondModel!
    
    var body: some View {
        VStack {
            HStack {
                Text(model.title)
                    .font(.system(size: 24, weight: .bold))
                    .frame(width: 160, alignment: .leading)
                    .foregroundColor(Color.white)
                Spacer()
                Image(model.logo)
            }
            Text(model.content.uppercased())
                .frame(maxWidth:.infinity ,alignment: .leading)
            model.contentImage
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 210)
        }
        .padding(.top,20)
        .padding(.horizontal,20)
        .frame(width: 275, height: 275)
        .background(model.bgColor)
        .clipShape(RoundedRectangle(cornerRadius: 20))
        .shadow(color: model.bgColor.opacity(0.4), radius: 20, x: 0, y: 10)
               
    }
}

